package com.gistmap.cloudservicegateway.util;

import com.gistmap.cloudservicegateway.comm.BaseException;
import com.gistmap.cloudservicegateway.comm.ResultEnum;
import com.google.common.collect.ImmutableSet;
import com.netflix.zuul.context.RequestContext;

import java.util.HashSet;
import java.util.Set;

/**
 * @author zhangran
 * @date 2018/6/22
 */
public class ResponseUtil {

    public static final String ACCESS_TOKEN = "access-token";
    public static final Set<String> WHITE_LIST = new HashSet<>();
    public static final Set<String> METHODS = ImmutableSet.of("GET", "DELETE");

    static {
        WHITE_LIST.add("cloud-main-api/token");
        WHITE_LIST.add("cloud-main-api/token/code");
        WHITE_LIST.add("cloud-main-api/token/guest");
        WHITE_LIST.add("cloud-main-api/user/password");
        WHITE_LIST.add("cloud-main-api/common/config");
        WHITE_LIST.add("cloud-main-api/common/feedback");
        WHITE_LIST.add("cloud-main-api/common/qiniu");
        WHITE_LIST.add("cloud-main-api/sms");
        WHITE_LIST.add("cloud-main-api/sms/verification");
    }
    public static void deny(RequestContext ctx, ResultEnum error) {
        ctx.setSendZuulResponse(false);
        ctx.setResponseStatusCode(400);
        ctx.getResponse().setContentType("application/json; charset=utf-8");
        ctx.setResponseBody(JsonConverter.toJson(new BaseException(error)));
        ctx.set("isSuccess", false);
    }

    public static void routing(RequestContext ctx) {
        ctx.setSendZuulResponse(true);
        ctx.setResponseStatusCode(200);
        ctx.set("isSuccess", true);
    }

    public static boolean isWhiteList(String url){
        return METHODS.stream().anyMatch(url::contains);
    }
}
